package com.Lyf.user.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.Lyf.user.config.RabbitMQConstant.DELAY_EXCHANGE;
import static com.Lyf.user.config.RabbitMQConstant.DELAY_EXCHANGE_TO_DELAY_QUEUE_A_ROUTING_KEY;

//step1:配置类
@Configuration
//step2:开启定时任务
@EnableScheduling
@Slf4j
public class ConfigureTask {



    @Autowired
    RabbitTemplate rabbitTemplate;

    //step3:添加定时任务(此处表示每5s执行一次),次数使用cron表达式
    @Scheduled(cron = "0/30 * * * * ?")
    private void test()
    {
        //向消息队列中写一条消息,此处是基于rabbitTemplate
        //交换机名字是啥+发到哪个延迟队列(routingKey要写对)+要写入啥消息
        System.out.println("写入-----------------------"+count());
        rabbitTemplate.convertAndSend(DELAY_EXCHANGE,DELAY_EXCHANGE_TO_DELAY_QUEUE_A_ROUTING_KEY,getCurrentTime());
    }

    //获取系统当前时间
    public String getCurrentTime()
    {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public int count()
    {
        int count=0;
        return count+1;
    }

}
